home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / time / RCS / mktime.c,v < prev    next >
Encoding:
Text File  |  1990-05-03  |  2.9 KB  |  141 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     90.05.03.16.45.54;  author rab;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/* 
  26.  * mktime.c --
  27.  *
  28.  *    Source code for the "mktime" library routine.
  29.  *
  30.  * Copyright 1988 Regents of the University of California
  31.  * Permission to use, copy, modify, and distribute this
  32.  * software and its documentation for any purpose and without
  33.  * fee is hereby granted, provided that the above copyright
  34.  * notice appear in all copies.  The University of California
  35.  * makes no representations about the suitability of this
  36.  * software for any purpose.  It is provided "as is" without
  37.  * express or implied warranty.
  38.  */
  39.  
  40. #ifndef lint
  41. static char rcsid[] = "$Header$";
  42. #endif
  43.  
  44. #include <time.h>
  45.  
  46. #ifdef __STDC__
  47. static int comparTime(struct tm *t1, struct tm *t2);
  48. #else
  49. static int comparTime();
  50. #endif
  51.  
  52. /*
  53.  *----------------------------------------------------------------------
  54.  *
  55.  * mktime --
  56.  *
  57.  *      Convert a local time, contained in a `struct *tp' into a
  58.  *      calander time in the same representation used by time(2).
  59.  *
  60.  * Results:
  61.  *      Returns the calander time, or -1 if it cannot be represented.
  62.  *
  63.  * Side effects:
  64.  *      Trashes localtime's internal buffer.
  65.  *
  66.  *----------------------------------------------------------------------
  67.  */
  68.  
  69. time_t
  70. mktime(tp)
  71.     struct tm *tp;
  72. {
  73.     register int i, r;
  74.     time_t t = 0;
  75.     struct tm temp;
  76.  
  77.     /* Make a local copy just in case tp points to localtime's buffer */
  78.     temp = *tp;
  79.     /* Check for zero */
  80.     if (comparTime(&temp, localtime(&t)) == 0) {
  81.     return 0;
  82.     }
  83.     /* Do a binary search, until the right time is found */
  84.     for (i = 31; --i >= 0;) {   /* start with high bit, work toward zero */
  85.     t |= 1L << i;           /* set bit, then compare times */
  86.     if ((r = comparTime(&temp, localtime(&t))) < 0) {
  87.         t &= ~(1L << i);    /* t is later, so clear the bit */
  88.     } else if (r == 0) {
  89.         return t;    /* times match, so return */
  90.     }
  91.     }
  92.     /* If we get here, then tp didn't point to valid data. */
  93.     return -1;
  94. }
  95.  
  96. /*
  97.  *------------------------------------------------------------------
  98.  * comparTime --
  99.  *
  100.  *      Compare two `struct tm's.
  101.  *
  102.  * Results:
  103.  *      0 if the times are the same.
  104.  *      Positive if t1 is later than t2.
  105.  *      Negative if t1 is earlier than t2.
  106.  *
  107.  * Side effects:
  108.  *      None.
  109.  *
  110.  *-----------------------------------------------------------------
  111.  */
  112.  
  113. static int
  114. comparTime(t1, t2)
  115.     struct tm *t1, *t2;
  116. {
  117.     int diff;
  118.  
  119.     if ((diff = t1->tm_year - t2->tm_year) != 0) {
  120.     return diff;
  121.     }
  122.     if ((diff = t1->tm_mon - t2->tm_mon) != 0) {
  123.     return diff;
  124.     }
  125.     if ((diff = t1->tm_mday - t2->tm_mday) != 0) {
  126.     return diff;
  127.     }
  128.     if ((diff = t1->tm_hour - t2->tm_hour) != 0) {
  129.     return diff;
  130.     }
  131.     if ((diff = t1->tm_min - t2->tm_min) != 0) {
  132.     return diff;
  133.     }
  134.     if ((diff = t1->tm_sec - t2->tm_sec) != 0) {
  135.     return diff;
  136.     }
  137.     return 0;
  138. }
  139.  
  140. @
  141.